package wcg;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.Point;

import javax.swing.JDialog;
import javax.swing.JCheckBox;
import java.awt.Rectangle;
import javax.swing.JButton;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.BoxLayout;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class CsvSigmaOptionDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JCheckBox jCheckBoxConvSNR = null;
	private JCheckBox jCheckBoxHeader = null;
	private JCheckBox jCheckBoxTime = null;
	private JButton jButtonCancel = null;
	private JButton jButtonOK = null;
	public  boolean Header = true;
	public  boolean Time = true;
	public  boolean ConvSNR = false;
	public  boolean OK=false;
	private JTextField textFieldNormalDataId;
	public  int NormalDataCh = 0xfff0;
	private JPanel panel_1;
	private JPanel panel_2;
	private JPanel panel_3;
	private JCheckBox chckbxUseNormalData;
	public  boolean IsNormalData  = false;
	private JPanel panel_4;
	private JLabel lblFilter;
	private JComboBox comboBoxFilter;
	public  int FilterMode = 0;
	
	public void updateData(boolean set){
		if(set){
			getJCheckBoxHeader().setSelected(Header);
			getJCheckBoxTime().setSelected(Time);
			getJCheckBoxConvSNR().setSelected(ConvSNR);
			chckbxUseNormalData.setSelected(IsNormalData);
			textFieldNormalDataId.setText(String.format("%04x", NormalDataCh));
			comboBoxFilter.setSelectedIndex(FilterMode);
		}
		else{
			Header = getJCheckBoxHeader().isSelected();
			Time   = getJCheckBoxTime().isSelected();
			ConvSNR= getJCheckBoxConvSNR().isSelected();
			IsNormalData = chckbxUseNormalData.isSelected();
			NormalDataCh = Integer.valueOf(textFieldNormalDataId.getText(),16);
			FilterMode = comboBoxFilter.getSelectedIndex();
		}
	}
	/**
	 * @param owner
	 */
	public CsvSigmaOptionDialog(Frame owner) {
		super(owner,true);
		initialize();
		if(owner!=null){
			Point p = owner.getLocationOnScreen();
			Dimension d = owner.getSize();
			Dimension dm = this.getSize();
			p.x += (d.width-dm.width)/2;
			p.y += (d.height-dm.height)/2;
			setLocation(p);
		}
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(301, 306);
		this.setTitle("Sigma CSV Output option");
		this.setContentPane(getJContentPane());
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE );
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			
			JPanel panel = new JPanel();
			panel.setBounds(47, 225, 160, 28);
			jContentPane.add(panel);
			panel.setLayout(new GridLayout(0, 2, 0, 0));
			panel.add(getJButtonCancel());
			panel.add(getJButtonOK());
			jContentPane.add(getPanel_2());
		}
		return jContentPane;
	}

	/**
	 * This method initializes jCheckBoxConvSNR	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxConvSNR() {
		if (jCheckBoxConvSNR == null) {
			jCheckBoxConvSNR = new JCheckBox();
			jCheckBoxConvSNR.setText("Convert sigma into SNR");
			jCheckBoxConvSNR.setToolTipText("Output sigma(digit) or SNR 20*log(sigma/FullScale");
		}
		return jCheckBoxConvSNR;
	}

	/**
	 * This method initializes jCheckBoxHeader	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxHeader() {
		if (jCheckBoxHeader == null) {
			jCheckBoxHeader = new JCheckBox();
			jCheckBoxHeader.setText("Header");
			jCheckBoxHeader.setToolTipText("Output Header column");
		}
		return jCheckBoxHeader;
	}

	/**
	 * This method initializes jCheckBoxTime	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxTime() {
		if (jCheckBoxTime == null) {
			jCheckBoxTime = new JCheckBox();
			jCheckBoxTime.setText("Time");
			jCheckBoxTime.setToolTipText("Output Time field");
		}
		return jCheckBoxTime;
	}

	/**
	 * This method initializes jButtonCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("CANCEL");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					OK = false;
					setVisible(false);
				}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jButtonOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");
			jButtonOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					OK = true;
					setVisible(false);
				}
			});
		}
		return jButtonOK;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new GridLayout(5, 1, 0, 0));
			panel_1.add(getJCheckBoxHeader());
			panel_1.add(getJCheckBoxTime());
			panel_1.add(getJCheckBoxConvSNR());
			panel_1.add(getPanel_3());
			panel_1.add(getPanel_4());
		}
		return panel_1;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setBounds(12, 10, 261, 150);
			panel_2.setLayout(new BorderLayout(0, 0));
			panel_2.add(getPanel_1(), BorderLayout.CENTER);
		}
		return panel_2;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setLayout(new BorderLayout(10, 0));
			
			textFieldNormalDataId = new JTextField();
			textFieldNormalDataId.setText("fff0");
			textFieldNormalDataId.setEnabled(false);
			panel_3.add(textFieldNormalDataId, BorderLayout.EAST);
			textFieldNormalDataId.setFont(new Font("MS UI Gothic", Font.PLAIN, 15));
			textFieldNormalDataId.setHorizontalAlignment(SwingConstants.CENTER);
			textFieldNormalDataId.setColumns(10);
			
			chckbxUseNormalData = new JCheckBox();
			panel_3.add(chckbxUseNormalData, BorderLayout.WEST);
			chckbxUseNormalData.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(chckbxUseNormalData.isSelected()){
						textFieldNormalDataId.setEnabled(true);
					}
					else{
						textFieldNormalDataId.setEnabled(false);
					}
							
				}
			});
			chckbxUseNormalData.setToolTipText("Output sigma(digit) or SNR 20*log(sigma/FullScale");
			chckbxUseNormalData.setText("Use normal data CH");
		}
		return panel_3;
	}
	private JPanel getPanel_4() {
		if (panel_4 == null) {
			panel_4 = new JPanel();
			panel_4.setLayout(new BorderLayout(10, 0));
			
			comboBoxFilter = new JComboBox();
			panel_4.add(comboBoxFilter, BorderLayout.CENTER);
			comboBoxFilter.setModel(new DefaultComboBoxModel(new String[] {"None", "Median 3", "Median 5", "Median 11", "Median 31"}));
			panel_4.add(getLblFilter(), BorderLayout.WEST);
		}
		return panel_4;
	}
	private JLabel getLblFilter() {
		if (lblFilter == null) {
			lblFilter = new JLabel("Filter");
		}
		return lblFilter;
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
